package com.qf.air.filter;

import com.netflix.zuul.ZuulFilter;


import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Component;

import javax.jnlp.FileContents;
import javax.servlet.http.HttpServletRequest;
import java.util.logging.Filter;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/11/11 20:49
 */
@Component
public class AuthenticationFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        // 推荐采用常量中提供的过滤器顺序进行加，减操作设置顺序
        // PRE_DECORATION_FILTER_ORDER = 5，是Zuul自带的前缀过滤器的最后一个过滤器
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1000;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext=RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String token = request.getParameter("token");
        if (!"123".equalsIgnoreCase(token)){
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
        }

        //凡回结果没有意义
        return null;
    }
}
